package server.views.servermngpanel.toppanel;
/**
 * 验证线程,发上线包,刷新上线信息
 */
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import sjtu.DCL.GM.common.Message;
import sjtu.DCL.GM.server.Public;
import sjtu.DCL.GM.server.ServerMain;
import sjtu.DCL.GM.server.dao.DaoFactory;
import sjtu.DCL.GM.server.dao.GoodsDao;
import sjtu.DCL.GM.server.dao.UserDao;



public class ValidateThread implements Runnable {

	private Socket s;

	private Message clientPacket;
	private Message serverPacket;
	private Message upLinePacket;
	private boolean success = false;
	private ServerMain f;
	private UserDao userDao=DaoFactory.newInstance().createUserDao();
	private GoodsDao goodsDao=DaoFactory.newInstance().createGoodsDao();
	private HashMap hashMap=new HashMap();
	public ValidateThread(Socket s,ServerMain f) {
		super();
		this.s = s;
		this.f=f;
	}

	public void run() {
		ObjectInputStream ois = null;
		ObjectOutputStream oos = null;
		try {
			ois = new ObjectInputStream(new BufferedInputStream(s
					.getInputStream()));
			clientPacket = (Message) ois.readObject();
			// 判断包的类型
			String type = clientPacket.getType();
			System.out.println(clientPacket.getType());
			// 验证登录包是否正确
			if (type.equals(Message.Login_type)) {
				String id = (String) clientPacket.getData().get("userNo");//登录的用户id 
				String pwd = (String) clientPacket.getData().get("userPwd");
				hashMap.put("5", id);
				int count = userDao.selectUser(hashMap).size();
				hashMap.clear();
				HashMap tempData = new HashMap();
				// 发验证包
				if (count > 0) {
					// 判断用户是否已登录了
					hashMap.put("6", id);
					int onLine = Integer.parseInt((String)userDao.selectUser(hashMap).get(0));
					hashMap.clear();
					if (onLine == 0) {
						// 不在线
						// 返回数据库中用户密码
						hashMap.put("7", id);
						String st = (String) userDao.selectUser(hashMap).get(0);
						hashMap.clear();
						if (st.equals(pwd)) {
							// 验证通过
							// 修改用户信息,把是否在线字段修改成1
							userDao.updateOnLine(1, id);
							tempData.put("tag", "4");
							ArrayList data = new ArrayList();
							
							// 取该用户的好友和好友群信息，发送到客户端						
//							Vector vUser = userDao.getFriendList(id);						
//							Vector vGroup =userDao.getFriendGroupList(id);
							
							HashMap h = new HashMap();
							h.put("1", "");
							Vector vDept = goodsDao.selectGoods(h);
							
////						存所有用户的，部门的信息，发送到客户端
//							hashMap.put("8", "");
//							Vector vUser = userDao.selectUser(hashMap);
//							hashMap.clear();
//							h.put("1", "");
//							Vector vDept =dao.selectDept(h);
//							h.clear();
							
//							data.add(vUser);
							data.add(vDept);
//							data.add(vGroup);
							tempData.put("data", data);
							success=true;
						} else {
							// 发密码不正确的验证包
							tempData.put("tag", "1");
							tempData.put("data", "密码错误");
						}
					} else {
						// 发号码已登录的验证包
						tempData.put("tag", "2");
						tempData.put("data", "此[" + id + "]号码已登录");
					}
				} else {
					// 发号码不存在的验证包
					tempData.put("tag", "0");
					tempData.put("data", "此[" + id + "]号码不存在");
				}
				serverPacket = new Message(Message.ValiDate_type,
						Message.Server_Id, id, tempData);
				oos = new ObjectOutputStream(new BufferedOutputStream(s
						.getOutputStream()));
				oos.writeObject(serverPacket);
				oos.flush();

				// 判断是否登录成功
				if (success == true) {
					TopPanel.setTShow(id,4);
					// 创建线程,接收客户端信息
					ReceiveThread recieveThread = new ReceiveThread(id,ois,f);
					Thread myRecieveThread = new Thread(recieveThread);
					myRecieveThread.start();
					// 保存信息到全局变量
					ArrayList arryList = new ArrayList();
					arryList.add(myRecieveThread);
					arryList.add(s);
					arryList.add(oos);
					arryList.add(ois);
					hashMap.put("10", id);
					//arryList.add((String)userDao.selectUser(hashMap).get(0));//用户所在部门编号
					hashMap.clear();
					Public.mySocketInfo.put(id, arryList);
					// 修改用户信息,把是否在线字段修改成1
					userDao.updateOnLine(1, id);
					// 向所有用户发上线包
					// 1.打包
					upLinePacket = new Message(Message.up_line_type, id,
							"", null);
					// 2.取出所有的QQ用户的输出流
					Set set = (Set) Public.mySocketInfo.keySet();
					Iterator iter = set.iterator();
					String[] QQ_id = new String[set.size()];
					int i = 0;
					while (iter.hasNext()) {
						QQ_id[i] = (String) iter.next();
						if (!QQ_id[i].equals(id)) {
							ArrayList a = (ArrayList) Public.mySocketInfo
									.get(QQ_id[i]);
							ObjectOutputStream tempOos = (ObjectOutputStream) a
									.get(2);//取出list里的输出流
							tempOos.writeObject(upLinePacket);//发出上线包
							tempOos.flush();
						}
						i++;
					}
					//更新显示在服务器端的上线用户信息
					hashMap.put("9", "");
					Vector vector = userDao.selectUser(hashMap);
					hashMap.clear();
					f.getServerMngPanel().getTablePanel().getDataModel().setData(vector);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} 
	}

}
